package com.atguigu.statistics.service.impl;

import com.atguigu.commonUtils.R;
import com.atguigu.statistics.entity.Daily;
import com.atguigu.statistics.feign.UcenterClient;
import com.atguigu.statistics.mapper.DailyMapper;
import com.atguigu.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-22
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 统计某一天的注册人数，生成统计数据
     *
     * @param day
     */
    @Override
    public void registerCount(String day) {
        //添加记录之前，先删除表里相同日期的旧数据 再进行添加 避免创建多条同一日期的统计数据
        baseMapper.delete(new QueryWrapper<Daily>().eq("date_calculated", day));

        //远程调用 得到某一天的注册人数
        R registerR = ucenterClient.countRegister(day);

        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        Daily daily = new Daily();
        daily.setRegisterNum(countRegister);//注册人数
        daily.setDateCalculated(day); //统计日期

        daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        daily.setLoginNum(RandomUtils.nextInt(100, 200));
        daily.setCourseNum(RandomUtils.nextInt(100, 200));


        //把获取到的数据，添加到数据库 统计分析表里面
        baseMapper.insert(daily);
    }

    /**
     * 图表显示，返回两部分数据，日期json数组，数量json数组
     *
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        //根据条件 查询对应的胡数据
        List<Daily> staList = baseMapper.selectList(new QueryWrapper<Daily>().between("date_calculated", begin, end).select("date_calculated", type));


        //返回数据有两部分数据，日期 和 日期对应数量
        //前端要求 数组 json结构，对应后端 java代码 是list集合

        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据 list集合，进行封装
        for (Daily daily : staList) {
            //封装日期的list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应的数量 list集合
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        //把封装之后的两个list集合 放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", date_calculatedList);
        map.put("numDataList", numDataList);

        return map;
    }
}
